package com.ls.socketio.controller;

import com.corundumstudio.socketio.SocketIOServer;
import com.ls.socketio.bean.ListClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author liShan
 * @date 2019/9/2 12:49
 */
@RestController
@RequestMapping("/socketIo")
public class SocketIoController {

    @Resource
    public SocketIOServer socketIoServer;
    private List<UUID> listClient = ListClient.getInstance().getListClients();

    @GetMapping("/{val}")
    public String send(@PathVariable("val") String val){
        this.sendBuyLogEvent(val);
        return "ok";
    }

    /**
     * 这里就是向客户端推消息了
     */
    private void sendBuyLogEvent(String val) {
        String dateTime = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss").format(new Date());
        for (UUID clientId : listClient) {
            if (socketIoServer.getClient(clientId) == null) {
                continue;
            }
            socketIoServer.getClient(clientId).sendEvent("enewbuy", dateTime + val, 1);
        }
    }

}
